package com.hollycrm.hollysqm.configur;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import com.hollycrm.hollysqm.bean.FtpConfigurBean;
import com.hollycrm.hollysqm.bean.VocFtpSourceBean;
import com.hollycrm.hollysqm.bean.XmlFtpSourceBean;

/**
 * FTP参数配置类
 * @author jianglong
 * @date 2017年3月6日 上午11:12:29
 */
@Configuration
public class FtpSourceConfig implements EnvironmentAware {

	private RelaxedPropertyResolver propertyResolver;
	
	private final static Logger log = LoggerFactory.getLogger(FtpSourceConfig.class);
	
	static {
		log.info("创建FTP|SFTP下载服务连接对象...");
	}
	
	/**
	 * 实现方法，获取Spring加载的资源数据
	 */
	@Override
	public void setEnvironment(Environment env) {
		this.propertyResolver = new RelaxedPropertyResolver(env);
	}
	
	/**
	 * 加载XML的FTP资源信息，并返回数据封装后的Bean对象
	 * @return
	 */
	@Bean(name="xmlFtpSourceBean")
	public XmlFtpSourceBean getXmlFtpSourceBean(){
		XmlFtpSourceBean ftpBean = new XmlFtpSourceBean();
		String ftp="ftp.xmlserver";
		String count = propertyResolver.getProperty(ftp + ".count");
		int c = StringUtils.isNotBlank(count)?Integer.parseInt(count):0;
		List<FtpConfigurBean> ftpList=new ArrayList<>();
		for (int i=1;i<=c;i++){
			ftpList.add(getFtpSource(ftp,i));
		}
		ftpBean.setFtpConfigurList(ftpList);
		return ftpBean;
	}
	
	/**
	 * 加载VOC的FTP资源信息，并返回数据封装后的Bean对象
	 * @return
	 */
	@Bean(name="vocFtpSourceBean")
	public VocFtpSourceBean getVocFtpSourceBean(){
		VocFtpSourceBean ftpBean = new VocFtpSourceBean();
		String ftp="ftp.vocserver";
		String count = propertyResolver.getProperty(ftp + ".count");
		int c = StringUtils.isNotBlank(count)?Integer.parseInt(count):0;
		List<FtpConfigurBean> ftpList=new ArrayList<>();
		for (int i=1;i<=c;i++){			
			ftpList.add(getFtpSource(ftp,i));
		}
		ftpBean.setFtpConfigurList(ftpList);
		return ftpBean;
	}
	
	/**
	 * 加载FTP配置，支持多服务器
	 * @param ftp
	 * @param i
	 * @return
	 */
	public FtpConfigurBean getFtpSource(String ftp ,int i){
		FtpConfigurBean bean = new FtpConfigurBean();		
		String port = propertyResolver.getProperty(ftp+i+".port");//端口
		int p = StringUtils.isNotBlank(port)?Integer.parseInt(port):21;
		String index = propertyResolver.getProperty(ftp+i+".index");//第几个服务器
		int ind = StringUtils.isNotBlank(index)?Integer.parseInt(index):1;
		bean.setIndex(ind);
		bean.setParentPath(propertyResolver.getProperty(ftp+i+".parentPath"));//下载目录
		bean.setHost(propertyResolver.getProperty(ftp+i+".host"));//下载主机
		bean.setPort(p);
		bean.setUserName(propertyResolver.getProperty(ftp+i+".userName"));//用户名
		bean.setPassword(propertyResolver.getProperty(ftp+i+".password"));//密码
		bean.setType(propertyResolver.getProperty(ftp+i+".type"));//FTP类型
		return bean;
	}
	
	
}
